<template>
  <div class="model-box">
    <template v-if="listsData.length">
      <div v-for="(list, index) in listsData" :key="index" class="list-box">
        <component
          :is="list.componentName"
          @onChange="
            (v) => {
              changeData(v, index)
            }
          "
          :list="list"
          :index="index + 1"
          :ref="list.componentName"
          @deleteModel="deleteModel(index)"
        >
          <div class="bu-group">
            <el-button
              type="primary"
              @click="addFirst"
              icon="el-icon-plus"
              plain
              >增加页面</el-button
            >
            <el-button type="danger" @click="deleteModel(index)" plain
              >删除</el-button
            >
          </div>
        </component>
      </div>
    </template>
    <template v-else>
      <div>
        <div class="pre-box bg" @click="addFirst">
          <div class="ifnull">
            <i class="el-icon-circle-plus-outline"></i>
            添加模板
          </div>
        </div>
      </div>
    </template>
    <ChooseModel ref="chooseModel" @addTypeData="addTypeData"></ChooseModel>
  </div>
</template>

<script>
import ChooseModel from './ChooseModel'
import LargeScreen from '../ModelType/LargeScreen'
import modules from '../ModelType/exportModel.js'

export default {
  components: {
    ChooseModel,
    LargeScreen,
    ...modules
  },
  props: {
    listsData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {}
  },
  watch: {
    listsData: {
      handler: function (n, o) {
        // this.$emit('onChange', n)
      },
      deep: true
    }
  },
  methods: {
    changeData(v, index) {
      this.$set(this.listsData, index, {
        ...this.listsData[index],
        ...v
      })
      this.$emit('onChange', this.listsData)
    },
    deleteModel(index) {
      this.listsData.splice(index, 1)
      this.$emit('onChange', this.listsData)
    },

    addTypeData(v) {
      // type 0 为大屏 其他为文字模板
      v.componentName = v.type ? `ModelType${v.choosNum + 1}` : 'LargeScreen'
      this.listsData.push(v)
      this.$emit('onChange', this.listsData)
    },

    addFirst() {
      this.$refs.chooseModel.show()
    }
  }
}
</script>

<style lang="less" scoped>
.bu-group {
  text-align: right;
  vertical-align: bottom;
  display: inline-block;
}
.list-box {
  background: #f7f8fc;
  border-radius: 10px;
  min-height: 20px;
  box-sizing: border-box;
  padding: 10px;
  margin-bottom: 20px;
  padding: 10px 40px;
}

.name {
  margin-bottom: 16px;
}
.pre-box {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100%; /* 或任何具体值 */
  min-height: 160px;
  .ifnull {
    text-align: center;
    position: absolute;
    top: 50%;
    width: 100%;
    font-size: 18px;
    transform: translateY(-50%);
  }
}
.bg {
  background: #f5f8fb;
}
</style>
